import requests
import json
import pandas as pd

def judge_value(pd_title,pd_desc):
    base_url = 'http://10.40.4.158:8880/v1/workflows/run'
    api_key = 'app-7l6SFsDKGvlSDMZegch5evht'
    # 定制请求头
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json" 
    }

    # 定制请求体
    data = {
        "inputs": {
            "pd_title": f"{pd_title}",
            "pd_desc": f"{pd_desc}",
        },
        "response_mode": "blocking",
        "user": "abc-123"
            }

    # 发送post请求
    responses = requests.post(url = base_url , headers = headers , data = json.dumps(data))

    # 检查请求是否成功处理

    """
    返回的示例：
        {'task_id': '9c014150-e558-444a-84e9-e8712d4f1e7e', 
        'workflow_run_id': 'f256466b-6acf-46fb-aefb-0b9381f31893', 
        'data': 
        {'id': 'f256466b-6acf-46fb-aefb-0b9381f31893', 
        'workflow_id': 'f2198e5c-1eaf-428e-84fe-6967bd69e85a', 
        'status': 'succeeded', 
        'outputs': {'is_prohibited': '', 'requires_compliance_check': ''}, 
        'error': None, 'elapsed_time': 7.565172377973795, 
        'total_tokens': 2705, 'total_steps': 7, 'created_at': 1724752116, 
        'finished_at': 1724752123
        }
        }
    """

    if responses.status_code == 200:
        # print(responses.json()['outputs'])
        if 'data' in responses.json():
            result = responses.json()['data']['outputs']
            if result is not None:
                is_prohibited = result['is_prohibited']
                requires_compliance_check = result['requires_compliance_check']
                return is_prohibited,requires_compliance_check
            else:
                # return {
                #     'output键不存在':
                #     responses.json()['data']
                #     }
                return (1,1)
        else:
            return {
                "data键不存在":
                responses.json()
            }
    else:
        # print("请求错误",responses.status_code)
        # print("错误内容",responses.text)
        return {
            "请求错误":responses.status_code,
            "错误内容":responses.text
                 }

if __name__ == "__main__":
    chunk_size = 400
    list_tuples = []
    for chunk in pd.read_csv('D:\\python爬虫\\answer_sheet.csv' , chunksize=chunk_size):
    # for chunk in pd.read_csv('D:\\python爬虫\\测试表格.csv' , chunksize=chunk_size, sep=None, engine='python'):
        if chunk.shape[0] < chunk_size:
            # print("处理最后一块数据")
            # print(chunk)
            # print(chunk.shape)
            for row in chunk.itertuples():
            # 处理每一行数据
                # print(row[1],'------', row[2],'--------',row[3],'\n')  # 假设我们处理第二列和第三列
                result = judge_value(row[2],row[3])
                list_tuples.append(result)
    # print(list_tuples)
        else:
            for row in chunk.itertuples():
                # 处理每一行数据
                    # print(row[1],'------', row[2],'--------',row[3],'\n')  # 假设我们处理第二列和第三列
                    result = judge_value(row[2],row[3])
                    list_tuples.append(result)
# print(list_tuples)
    df1 = pd.DataFrame(list_tuples, columns=['is_prohibited','requires compliance check'])
    # 将DataFrame写入到CSV文件中，不包含索引
    df1.to_csv('D:\\python爬虫\\上海交大.csv', index=False)
